﻿using System;
using System.Drawing;
using System.Text;

namespace WooYang.Verification
{
    /// <summary>
    /// 验证码生成
    /// </summary>
    public class VerificationCode
    {
        private static string GenerateCheckCode(out string value)
        {
            int intFirst, intSec, intTemp;
            string checkCode = String.Empty;
            System.Random random = new Random();
            intFirst = random.Next(1, 30);
            intSec = random.Next(1, 10);
            switch (random.Next(1, 3).ToString())
            {
                case "2":
                    if (intFirst < intSec)
                    {
                        intTemp = intFirst;
                        intFirst = intSec;
                        intSec = intTemp;
                    }
                    checkCode = intFirst + "-" + intSec + "=？";
                  value = (intFirst - intSec).ToString();
                    break;
                default: checkCode = intFirst + "+" + intSec + "=？";
                    value =( intFirst + intSec).ToString();
                    break;
            }      
            return checkCode;
        }
        private static string NumberCode()
        {
            System.Random random = new Random();
            string code = "";
            for (int i = 0; i < 4; i++)
            {
                code += random.Next(0, 10).ToString();
            }
            return code;
        }
        private static string Code()
        {
            string codeSerial = "123456789abcdefghijkmnpqrstuvwxyz";
            System.Random random = new Random();
            string code = "";
            for (int i = 0; i < 4; i++)
            {
                code += codeSerial[random.Next(0, codeSerial.Length)].ToString();
            }
            return code;
        }
        private static string TextCode()
        {
            string codeSerial = @"的一国在人了有中是年和大业不为发会工经上地市要个产这出行作生家以成到日民来我部对进多全建他公开们场展时理新方主企资实学报制政济用同于法高长现本月定化加动合品重关机分力自外者区能设后就等体下万元社过前面";
            System.Random random = new Random();
            string code = "";
            for (int i = 0; i < 2; i++)
            {
                code += codeSerial[random.Next(0, codeSerial.Length)].ToString();
            }
            return code;
        }
        #region 产生波形滤镜效果
        private const double PI = 3.1415926535897932384626433832795;
        private const double PI2 = 6.283185307179586476925286766559;
        private static System.Drawing.Bitmap TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)
        {
            System.Drawing.Bitmap destBmp = new Bitmap(srcBmp.Width, srcBmp.Height);
            // 将位图背景填充为白色             
            System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(destBmp);
            graph.FillRectangle(new SolidBrush(System.Drawing.Color.White), 0, 0, destBmp.Width, destBmp.Height);
            graph.Dispose();
            double dBaseAxisLen = bXDir ? (double)destBmp.Height : (double)destBmp.Width;
            for (int i = 0; i < destBmp.Width; i++)
            {
                for (int j = 0; j < destBmp.Height; j++)
                {
                    double dx = 0;
                    dx = bXDir ? (PI2 * (double)j) / dBaseAxisLen : (PI2 * (double)i) / dBaseAxisLen;
                    dx += dPhase;
                    double dy = Math.Sin(dx);                       // 取得当前点的颜色              
                    int nOldX = 0, nOldY = 0;
                    nOldX = bXDir ? i + (int)(dy * dMultValue) : i;
                    nOldY = bXDir ? j : j + (int)(dy * dMultValue);
                    System.Drawing.Color color = srcBmp.GetPixel(i, j);
                    if (nOldX >= 0 && nOldX < destBmp.Width
                        && nOldY >= 0 && nOldY < destBmp.Height)
                    { destBmp.SetPixel(nOldX, nOldY, color); }
                }
            }
            return destBmp;
        }
        #endregion
        private static Bitmap CreateCheckCodeImage(string checkCode)
        {
            if (checkCode == null || checkCode.Trim() == String.Empty)
                return new Bitmap(15, 25);
            int width = Encoding.Default.GetBytes(checkCode).Length;
            System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((width* 15.0)), 25);
            Graphics g = Graphics.FromImage(image);
            try
            {                  //生成随机生成器                
                Random random = new Random();
                //清空图片背景色                 
                g.Clear(Color.White);
                //画图片的背景噪音线  
                int lengths = random.Next(5, 15);
                for (int i = 0; i < lengths; i++)
                {
                    int x1 = random.Next(image.Width);
                    int x2 = random.Next(image.Width);
                    int y1 = random.Next(image.Height);
                    int y2 = random.Next(image.Height);
                    g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
                }
                Font font = new System.Drawing.Font("Arial", 16, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
                System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
                g.DrawString(checkCode.Trim(), font, brush, 1, 1);
                //画图片的前景噪音点                 
                for (int i = 0; i < lengths*5; i++)
                {
                    int x = random.Next(image.Width);
                    int y = random.Next(image.Height);
                    image.SetPixel(x, y, Color.FromArgb(random.Next()));
                }
                image = TwistImage(image, true, 3, 1);//画图片的波形滤镜效果              
                //画图片的边框线                 
                g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
                return image;
            }
            finally
            {
                g.Dispose();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="way">验证码的方式，1- 加减法验证，2-数字,3-带字母,4-带汉字</param>
        /// <returns></returns>
        public static VCode GetVerificationCode(int way)
        {
            VCode item = new VCode();
           
            switch (way)
            {
                case 1:
                    string codevalue = "";
                    item.Picture=CreateCheckCodeImage(GenerateCheckCode(out codevalue));
                    item.Value = codevalue;
                    return item;
                case 2:
                    item.Value = NumberCode();
                    break;
                case 3:
                    item.Value = Code();
                    break;
                case 4:
                    item.Value = TextCode();
                    break;
                default:
                    item.Value = NumberCode();
                    break;
            }
            item.Picture = CreateCheckCodeImage(item.Value);
            return item;
        }
        
    }
    public class VCode
    {
        public string Value { get; set; }
        public Bitmap Picture { get; set; }

    }

}
